﻿using System;
using System.Collections.Generic;
using System.Linq;
using DataModel.Models;

namespace DataModel.Managers
{
    public class RoleManager
    {
        readonly UniversityPortalEntities _entityModel;

        public RoleManager()
        {
            _entityModel = new UniversityPortalEntities();
        }

        public void CreateRole(string name)
        {
            var role = new Role {ID = Guid.NewGuid(), name = name};

            _entityModel.AddToRoles(role);
            _entityModel.SaveChanges();
        }

        public RoleModel GetRole(Guid id)
        {
            var role = _entityModel.Roles.FirstOrDefault(rl => rl.ID == id);
            if (role != null)
            {
                var roleModel = new RoleModel {Name = role.name};
                return roleModel;
            }
            return null;
        }

        public RoleModel GetRole(string name)
        {
            var role = _entityModel.Roles.FirstOrDefault(rl => rl.name == name);
            if (role != null)
            {
                var roleModel = new RoleModel {Name = role.name};
                return roleModel;
            }
            return null;
        }

        public List<RoleModel> GetAllRoles()
        {
            var roles = new List<RoleModel>();
            foreach (var role in _entityModel.Roles)
            {
                roles.Add(new RoleModel { Name = role.name,ID = role.ID});
            }

            return roles;
        }
    }
}
